#include "../libxluploader.h"
#include "sites.h"
#include <assert.h>
#include <iostream>

std::string CSiteImageShack::parseImageUrl (const std::string &response) {
	if (!m_pcreCodePic) {
		return "";
	}

	const int size = 30;
	int ov[size] = {0};
	const char *text = response.c_str();
	int res = pcre_exec(m_pcreCodePic, NULL, text, response.length(), 0, 0, ov, size);
	if (res != 1) {
		return "";
	}

	std::string url = response.substr(ov[0], ov[1] - ov[0]);
	return url;
}

std::string CSiteImageShack::getUrl() const {
	return "http://imageshack.us/?no_multi=1";
}

CSiteImageShack::CSiteImageShack () {
	m_errPic = NULL;

 // we should extrack:
 // --- http://img403.imageshack.us/img403/350/17projects.png
	const char *pattern_pic = "http://img[\\d]*\\.imageshack\\.us/img\\d*/\\d*/[a-zA-Z0-9.]*";
	int flags = PCRE_BSR_ANYCRLF | PCRE_UTF8 | PCRE_MULTILINE | PCRE_DOTALL;
	m_pcreCodePic = pcre_compile(pattern_pic, flags, &m_errPic, &m_errPicOffset, NULL);
}

CSiteImageShack::~CSiteImageShack() {
	if (m_pcreCodePic) {
		pcre_free(m_pcreCodePic);
	}
}

TCHAR* CSiteImageShack::getSiteName () {
	return _T("ImageShack.us");
}

